package com.tianhao.luo.sync;

/**
 * @author tianhao.luo@hand-china.com
 */
public class DeadLockDemo {


    public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();


        new Thread(()->{
            synchronized (a){
                // 获取到a锁，并执行自己的操作，并且试图获取b锁
                System.out.println(Thread.currentThread().getName()+" 获取到了a的锁；准备获取b锁");
                try {
                    Thread.sleep(100);
                    synchronized (b){

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();



        new Thread(()->{
            synchronized (b){
                // 获取到a锁，并执行自己的操作，并且试图获取b锁
                System.out.println(Thread.currentThread().getName()+" 获取到了b的锁；准备获取a锁");
                try {
                    Thread.sleep(100);
                    synchronized (a){

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
